package com.zhgxi.zf.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slave4j.orm.PageData;
import org.slave4j.utils.WebUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;

import com.zhgxi.zf.entity.Menu;
import com.zhgxi.zf.service.MenuService;

@Controller("MenuAction")
public class MenuAction {

	@Resource
	private MenuService menuService;

	public List<Menu> findAll() {
		return menuService.findAllMenu();
	}

	public List<Menu> findByType(Boolean type) {
		return menuService.findByType(type);
	}

	public boolean save(Menu menu) {
		return menuService.saveMenu(menu);
	}

	public boolean deleteMenu(Menu m) {
		return menuService.deleteMenu(m);
	}

	public List<Menu> findByMname(String mname) {
		return menuService.findByMname(mname);
	}

	/* 以下方法不理 */
	public ModelMap list(HttpServletRequest request) {
		PageData<Menu> pageData = new PageData<Menu>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData = menuService.find(pageData);

		return new ModelMap(pageData);
	}

	public String addForm(Model model) {
		return "/zf/menu/input";
	}

	public String editForm(@PathVariable("id") Integer id, Model model) {
		Menu menu = menuService.find(id);
		model.addAttribute(menu);
		return "/zf/menu/input";
	}

	public String save2(Menu menu) {
		if (menu.isNew()) {
			menuService.save(menu);
		} else {
			menuService.update(menu);
		}

		return "redirect:/zf/menu/list";
	}

	public String delete(@PathVariable("id") Integer id) {
		menuService.delete(id);
		return "redirect:/zf/menu/list";
	}

	public String visible(@PathVariable("id") Integer id) {
		menuService.visible(id);
		return "redirect:/zf/menu/list";
	}

	public String unVisible(@PathVariable("id") Integer id) {
		menuService.unVisible(id);
		return "redirect:/zf/menu/list";
	}

	@InitBinder
	public void InitBinder(WebDataBinder dataBinder) {
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
	}

}
